package com.victor.controller;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.JMInfo;
import com.victor.service.JmAuditService;
import com.victor.utils.Msg;

/**
 * 	(改)加盟入驻审批（app后台）
 */
@RestController
@RequestMapping("/jmAaudit")
public class JmAuditController {
	
	@Autowired
	private JmAuditService jmAuditService;
	
	/**
	 * 	根据入驻审批状态值查询列表
	 * status为0，查询新的申请列表
	 * status为1，查询审批通过列表
	 * status为2，查询审批拒绝列表
	 * status为“”，查询所有列表
	 */
	@RequestMapping("/selectList")
	public Msg selectList(String status,int page,int row){
		if (status==null || status.equals("")) {
			return Msg.fail("参数错误");
		}
		PageInfo<JMInfo> list = jmAuditService.selectAll(status,page,row);
		return Msg.success().add("list", list); 
	}
	
	
	
	/**
	 * 	根据Id查看详情页信息
	 */
	@RequestMapping("/getMessage")
	public Msg getMessage(Integer id) {
		JMInfo detail = jmAuditService.selectById(id);
		if (detail != null ) {
			return Msg.success().add("detail", detail);
		}
		return Msg.fail();	
	}
	
	
	/**
	 *	 审批通过
	 * @return
	 */
	@RequestMapping("/pass")
	public Msg pass(Integer id) {
		boolean a = jmAuditService.pass(id);
		if (!a) {
			return Msg.fail();
		}
		return Msg.success();
	}
	
	/**
	 * 	审批拒绝
	 * @return
	 */
	@RequestMapping("/noPass")
	public Msg NoPass(Integer id) {
		boolean b = jmAuditService.noPass(id);
		if (!b) {
			return Msg.fail();
		}
		return Msg.success();
	}
	

	/**
	 *	 根据状态导出所有到表格
	 */
		@RequestMapping("/batchExport")
		public void batchExport(String status,HttpServletRequest request,HttpServletResponse response) throws Exception {
			//数据库查询数据 exportlist 需要导出的list
			List<JMInfo> list = jmAuditService.selectAllByStatus(status);
			// 创建poi导出数据对象
			SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
			// 创建sheet页
			Sheet sheet = sxssfWorkbook.createSheet("入驻列表");

			CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 8);
			// 参数1：起始行 参数2：终止行 参数3：起始列 参数4：终止列
			sheet.addMergedRegion(region1);
			Row headTitle = sheet.createRow(0);
			headTitle.createCell(0).setCellValue("入驻列表");

			// 创建表头
			Row headRow = sheet.createRow(5);
			// 设置表头信息
			headRow.createCell(0).setCellValue("入驻编号");
			headRow.createCell(1).setCellValue("申请时间");
			headRow.createCell(2).setCellValue("联系人");
			headRow.createCell(3).setCellValue("联系电话");
			headRow.createCell(4).setCellValue("入驻状态");//0待审批，1已通过，2已拒绝

			// 遍历上面数据库查到的数据
			for (JMInfo ji : list) {
				Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
				dataRow.createCell(0).setCellValue(ji.getId());
				dataRow.createCell(1).setCellValue(ji.getDate());
				dataRow.createCell(2).setCellValue(ji.getLinkman());
				dataRow.createCell(3).setCellValue(ji.getPhone());
				dataRow.createCell(4).setCellValue(ji.getStatus().equals("0") ? "待审批" : (ji.getStatus().equals("1") ? "已通过" : "已拒绝"));
			}
			// 下载导出文件名
			String filename = "入驻审批列表";
			// 设置头信息
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/vnd.ms-excel");
			// 一定要设置成xlsx格式
			response.setHeader("Content-Disposition",
					"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
			// 创建一个输出流
			ServletOutputStream outputStream = response.getOutputStream();
			// 写入数据
			sxssfWorkbook.write(outputStream);
			// 关闭
			outputStream.close();
			sxssfWorkbook.close();
		}

	
	/**
	 *	  批量删除入驻商家
	 */
	@RequestMapping("/batchDelete")
	public Msg batchDelete(int[] ids) {
		if (ids.length==0) {
			return Msg.fail("参数错误");
		}
		boolean b = jmAuditService.batchDelete(ids);
		if (b) {
			return Msg.success();
		}
		return Msg.fail();
	}
	
	/**
	 * 	search模糊搜索
	 * 	id date status username userid linkman phone zongclass
	 */
	@RequestMapping("/search")
	public Msg likeSearch(String search) {
		if (search==null) {
			return Msg.fail("参数错误");
		}
		List<JMInfo> list = jmAuditService.likeSearch(search);
		return Msg.success().add("list", list);
	}
}
